// pages/programme/watch-programme/watchProgramme.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBarHeight: app.globalData.navBarHeight,
    statusBarHeight: app.globalData.statusBarHeight,
    info: '',
    userName: '',
    productId: '',
    presentationList: [],
    allSelectList: [],
    scrollTop: 0,
    startPageY: 0,
    hasScroll: true,
    sortShow: false,
    flag:false,
    itemHeight: 90
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '查看方案'
    })
    this.setData({
      productId: options.id,
      navBarHeight: app.globalData.navBarHeight,
      statusBarHeight: app.globalData.statusBarHeight,
    })
    console.log(options.id)
    this.getWxmlCode('#moveBox',(resp)=>{
      console.log(resp)
      this.setData({moveBox:resp})
      setTimeout(() => {
        this.getWxmlCode('#movelist0',(res)=>{
          this.setData({movelist0:res})
          console.log(res.top, resp.top, res.bottom)
          var space = res.top-resp.top
          var half = res.bottom - res.top + space
          console.log(resp.top, res.bottom, res.top)
          this.setData({
              itemHeight: 90,
              space: half, //两条中间到另一条的距离
              beLeftSpace: resp.top-(res.bottom - res.top)/2, //位置要减去距离
            })
          })
      },300)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getMarkfirstViewSchemeDetail()
    this.getCustomerList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  reback () {
    wx.navigateBack({delta: 1})
  },
  edit () {
    wx.navigateTo({
      url: `/pages/programme/create-programme/createProgramme?type=edit&productId=${this.data.productId}`
    })
  },
  getCustomerList: function () {
    app.api.post({
      url: 'recommend_plan_detail',
      data: { productId: this.data.productId }
    }).then(res => {
      console.log(res)
      let arr = []
      if (res.presentationDetailResps.length > 0) {
        arr = res.presentationDetailResps
        this.setData({
          presentationList: arr,
          info: res,
        })
      } else {
        this.setData({
          presentationList: [],
          info: res
        })
      }
    })
  },
  toDemonstration () {
    console.log(this.data.presentationList)
    let presentationIds = []
    this.data.presentationList.forEach((v) => {
      if (v.id && !v.active) {
        presentationIds.push(v.id)
      }
    })
    if (presentationIds.length === 1) {
      if (this.data.presentationList[0].materialType === 'link') {
        let param = { save: false, presentationIds: presentationIds, userName: this.data.userName, productId: this.data.productId, customerId: this.data.customerId }
        app.api.post({
          url: 'material_presentation_start',
          data: param
        }).then(res => {
          console.log(res)
          wx.navigateTo({ url: `/pages/home-page/watch-project/watchProject?sensors_ext=${code}&url=${encodeURIComponent(res[0].fileResourceUrl)}` })
        })
      } else {
        wx.navigateTo({
          url: `/pages/home-page/to-demonstration/toDemonstration?from=material&type=demonstration&presentationIds=${presentationIds}&save=false&productId=${this.data.productId}`
        })
      }
    } else {
      wx.navigateTo({
        url: `/pages/home-page/to-demonstration/toDemonstration?from=material&type=demonstration&presentationIds=${presentationIds}&save=false&productId=${this.data.productId}`
      })
    }
  },
  goToDetail (e) {
    let presentationIds = [e.currentTarget.dataset.item.id]
    console.log(presentationIds)
    if (e.currentTarget.dataset.item.materialType === 'link') {
      wx.navigateTo({ url: `/pages/home-page/watch-project/watchProject?url=${encodeURIComponent(e.currentTarget.dataset.item.url)}`})
    } else {
      wx.navigateTo({
        url: `/pages/home-page/to-demonstration/toDemonstration?from=material&type=demonstration&presentationIds=${presentationIds}&save=false`
      }) 
    }
  },
  programmeToggle (e) {
    console.log(e)
    let { id } = e.currentTarget.dataset.item
    this.data.presentationList.forEach((v, k) => {
      if (id === v.id) {
        if (v.active) {
          console.log(1, v, this.data.allSelectList)
          v.active = false
          if (this.data.allSelectList.length > 0) {
            this.data.allSelectList.forEach((j, g) => {
              console.log(v, j)
              if (v.materialName === j.materialName) {
                this.data.allSelectList.splice(g, 1)
              }
            })
          }
        } else {
          v.active = true
          this.data.allSelectList.push(v)
        }
      }
    })
    console.log(this.data.allSelectList)
    this.setData({
      presentationList: this.data.presentationList,
      allSelectList: this.data.allSelectList
    })
  },
  listItemlongtap (e) {
    console.log('长按', e)
    this.setData({
      flag:true,
      hasScroll:false
    })
  },
  listItemStart (e) {
    console.log(e)
    this.setData({
      startPageY: e.touches[0].pageY
    })
    console.log(e.touches[0].pageY, this.data.itemHeight)
  },
  listItemMove (e) {
    if(this.data.flag){
      if (e.touches[0].pageY < this.data.itemHeight) {
        return false
      }
      console.log(e.touches[0].pageY, this.data.scrollTop)
      var moveTop = ((e.touches[0].pageY + this.data.scrollTop)-this.data.itemHeight - 172)
      if (moveTop < 0) {
        moveTop = 0
      }
      console.log(moveTop)
      var moveoutIndex = parseInt((moveTop+45)/this.data.itemHeight)
      console.log(moveoutIndex)
      this.moveoutIndex = moveoutIndex
      this.setData({nowmoveIndex:e.currentTarget.dataset.index,moveTop,moveoutIndex})
    }
  },
  scroll (e) {
    // console.log(e.detail.scrollTop)
    this.setData({
      scrollTop: e.detail.scrollTop
    })
  },
  listItemEnd(e){
    if (!this.data.flag) {
      return;
    }
    console.log(e.currentTarget.dataset.index, this.moveoutIndex)
    if (this.data.startPageY === e.changedTouches[0].pageY) {
      return false
    }
    let index = e.currentTarget.dataset.index
    let presentationList = this.data.presentationList
    let data = {...presentationList[index]}
    presentationList.splice(index,1)
    presentationList.splice(this.moveoutIndex,0,data)
    this.setData({presentationList:presentationList,moveoutIndex:-1,nowmoveIndex:-1, flag: false, hasScroll:true})
  },
  getWxmlCode(str,cal){
    const query = wx.createSelectorQuery()
    query.select(str).boundingClientRect()
    query.selectViewport().scrollOffset()
    query.exec((res) => {
    if(cal) cal(res[0])
    })
  },
  getMarkfirstViewSchemeDetail () {
    app.api.get({
      url: 'cpic_user_markFirstViewSchemeDetail',
      data: {}
    }).then(res => {
      app.globalData.userInfo.firstViewSchemeDetail = 1
      wx.setStorageSync("userInfo", JSON.stringify(app.globalData.userInfo))
      console.log(res)
    })
  },
  closeGuide () {
    this.setData({
      sortShow: false
    })
  }
})